package com.example.day02;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.example.MainActivity3;
import com.example.day02.p.Persenter;
import com.example.day02.v.View_Main;

import java.util.ArrayList;
import java.util.List;

public class MainActivity2 extends AppCompatActivity implements View_Main {

    private Toolbar mToob;
    private RecyclerView mRecy;
    private ArrayList<Bean> results;
    private Recy_Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        initView();
        Persenter persenter = new Persenter(this);
        persenter.getData();
    }

    private void initView() {
        mToob = (Toolbar) findViewById(R.id.toob);
        mRecy = (RecyclerView) findViewById(R.id.recy);
        mRecy.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mRecy.setLayoutManager(new LinearLayoutManager(this));
        results = new ArrayList<>();
        adapter = new Recy_Adapter(this, results);
        mRecy.setAdapter(adapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.img1:
                Intent intent1 = new Intent(MainActivity2.this, MainActivity4.class);
                startActivity(intent1);
                break;
            case R.id.img2:
                Intent intent = new Intent(MainActivity2.this, MainActivity3.class);
                startActivity(intent);
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void showData(List<Bean> resultsBeans) {
        results.addAll(resultsBeans);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(this, "str", Toast.LENGTH_SHORT).show();
    }
}
